package com.fangs.kotlin_banner.banner

import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import cn.jzvd.R
import com.fangs.kotlin_banner.utils.DisplayUtils

class MyVideoLayout : RelativeLayout, LifecycleObserver {
    var videoView: MyBannerVideoView? = null
    var imageView: ImageView? = null
    private var progressBar: ProgressBar? = null
    private var loop //是否循环播放
            = false
    private var videoCurrentPosition = 0
    var videoLimitTime = 0
    private var playing //标记是否是页面失去焦点时正在播放的视频 那么这个视频在获得焦点时需要恢复播放
            = false
    private var buffering //正在缓冲中
            = false

    constructor(context: Context, loop: Boolean) : this(context) {
        this.loop = loop
        init()
    }

    private constructor(context: Context) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        loop = true
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        loop = true
        init()
    }

    private fun init() {
        if (context is AppCompatActivity) {
            (context as AppCompatActivity).lifecycle.addObserver(this)
        }
        videoView = MyBannerVideoView(context, loop)
        videoView!!.setLayoutParams(
            LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT
            )
        )
        videoView!!.setShowLoadingListener(object : MyBannerVideoView.ShowLoadingListener {
            override fun showLoading(show: Boolean) {
                imageView!!.visibility = if (show) VISIBLE else GONE
                progressBar!!.visibility = if (show) VISIBLE else GONE
            }

            override fun showBuffering(show: Boolean) {
                progressBar!!.visibility = if (show) VISIBLE else GONE
                buffering = show
            }
        })
        addView(videoView)
        imageView = ImageView(context)
        imageView!!.scaleType = ImageView.ScaleType.CENTER_CROP
        imageView!!.layoutParams =
            LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        addView(imageView)
        progressBar = ProgressBar(context)
        val w: Int = DisplayUtils.dip2px(context, 45f)
        val layoutParams = LayoutParams(w, w)
        layoutParams.addRule(CENTER_HORIZONTAL, TRUE)
        layoutParams.addRule(CENTER_VERTICAL, TRUE)
        progressBar!!.indeterminateDrawable =
            context.resources.getDrawable(R.drawable.jz_loading) //此处借用饺子视频中的进度样式
        progressBar!!.layoutParams = layoutParams
        addView(progressBar)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onResume(lifecycleOwner: LifecycleOwner?) {
        if (playing) {
            // 笨办法 解决进入下一页后通过侧滑返回首页时 videoview异常问题(黑屏，显示不全)
            imageView!!.visibility = VISIBLE
            progressBar!!.visibility = VISIBLE
            videoView!!.setScaleError()
            videoView!!.seekTo(videoCurrentPosition)
            videoView!!.start()
            playing = false
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onPause(lifecycleOwner: LifecycleOwner?) {
        if (videoView!!.isPlaying() && !buffering) {
            playing = true
            videoCurrentPosition = videoView!!.getCurrentPosition()
            videoLimitTime = videoView!!.getDuration() - videoCurrentPosition
            videoView!!.pause()
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy(lifecycleOwner: LifecycleOwner?) {
        videoView!!.stopPlayback()
    }

    override fun onDetachedFromWindow() { //避免viewpager item view复用情况 视频黑屏
        super.onDetachedFromWindow()
        imageView!!.visibility = VISIBLE
        progressBar!!.visibility = VISIBLE
    }
}